/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


myApp.controller('postMealController',
        function($scope, $location, postMealService,$route,$rootScope) {
    
    $rootScope.showSearchBar = false;
    $rootScope.showWelcomeMessage = true;
    $rootScope.isLoginPage = true;
    var postMealSuccess = function() {
        //alert("Meal Posted successfully...");
        $rootScope.showAlert("Meal Posted successfully...");
        $route.reload();
    }
    var postMealFailure = function() {
        console.log("Failure while posting meal.");
    }
    
    $scope.postData = function(){
        var data = {};
        
        if(! $scope.mealName){
            $rootScope.showAlert("Please enter Meal Name.");
            return;
        }        
        data.mealName = $scope.mealName;
        
        if(! $scope.price){
            $rootScope.showAlert("Please enter Meal Price.");
            return;
        }
        data.price = $scope.price;
        
        if(! $scope.quantity){
            $rootScope.showAlert("Please select Quantity.");
            return;
        }
        data.quantity = $scope.quantity;
        
        if(! $scope.mealItems){
            $rootScope.showAlert("Please enter at least one item.");
            return;
        }
        data.mealItems = $scope.mealItems;
        
        if(! $scope.mealAddress1){
            $rootScope.showAlert("Please enter the Address.");
            return;
        }
        data.address = $scope.mealAddress1 + ' ' + $scope.mealAddress2;
        
        if(! $scope.createdOn){
            $rootScope.showAlert("Please select a date.");
            return;
        }
        data.createdOn = $scope.createdOn;
        
        if(! $scope.mealType){
            $rootScope.showAlert("Please select meal type.");
            return;
        }
        data.mealType = $scope.mealType
        
        
        data.chefSpecial = $scope.chefSpecial;
        
        data.description = $scope.description;
        
        postMealService.postMeal(data).then(postMealSuccess, postMealFailure);
    };
    
    $scope.selectMealType = function(mealType){
        $scope.mealType = mealType;
    }
    
    $scope.cancel = function() {
        $location.path("/login" );
    }
    
});